﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Net.Sz.Framework.ExcelTools.CreateCode.protobuf
{
    /// <summary>
    /// 
    /// <para>PS:</para>
    /// <para>@author 失足程序员</para>
    /// <para>@Blog http://www.cnblogs.com/shizuchengxuyuan/</para>
    /// <para>@mail 492794628@qq.com</para>
    /// <para>@phone 13882122019</para>
    /// </summary>
    public class CreateJavaProtobuf
    {
        /// <summary>
        /// proto 3
        /// </summary>
        /// <param name="files"></param>
        /// <param name="show"></param>
        public static void CreateCode3(List<ItemFileInfo> files, Action<string> show)
        {
            try
            {
                using (Process process = new Process())
                {
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.FileName = "protobuf/protoc3.exe";
                    string out_dir = "";
                    foreach (var item in files)
                    {
                        if (item.ExtensionName.Equals(".proto"))
                        {
                            show("开始处理 Protobuf 3 文件：" + item.Name + " 生成 Java 文件");
                            string fileNameWithoutExtension = Path.GetFileName(item.Path);
                            out_dir = Path.GetDirectoryName(item.Path);
                            if (string.IsNullOrWhiteSpace(CreateBase.protobufjavaMessage))
                            {
                                if (!System.IO.Directory.Exists(out_dir + "/out/java/"))
                                {
                                    System.IO.Directory.CreateDirectory(out_dir + "/out/java/");
                                }
                                process.StartInfo.Arguments = "--proto_path=" + out_dir + " --java_out=" + out_dir + "/out/java   " + item.Path;
                            }
                            else
                            {
                                if (!System.IO.Directory.Exists(CreateBase.protobufjavaMessage))
                                {
                                    System.IO.Directory.CreateDirectory(CreateBase.protobufjavaMessage);
                                }
                                process.StartInfo.Arguments = "--proto_path=" + out_dir + " --java_out=" + CreateBase.protobufjavaMessage + "   " + item.Path;
                            }
                            process.Start();
                            string error = process.StandardError.ReadToEnd();
                            if (!string.IsNullOrWhiteSpace(error))
                            {
                                show("java文件:" + fileNameWithoutExtension + "   错误:" + error);
                                return;
                            }
                        }
                    }
                    //process.WaitForExit();
                    process.Close();
                    show("");
                    show("");
                    show("创建 protobuf 3 Java 代码完成 目录：" + out_dir);
                    show("");
                    //System.Diagnostics.Process.Start(CreateBase.protobufjavaMessage);
                }
            }
            catch (Exception e)
            {
                show(e.Message);
            }
        }

        /// <summary>
        /// proto 2
        /// </summary>
        /// <param name="files"></param>
        /// <param name="show"></param>
        public static void CreateCode(List<ItemFileInfo> files, Action<string> show)
        {
            try
            {
                using (Process process = new Process())
                {
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.FileName = "protobuf/protoc.exe";
                    string out_dir = "";
                    foreach (var item in files)
                    {
                        if (item.ExtensionName.Equals(".proto"))
                        {
                            show("开始处理 Protobuf 2 文件：" + item.Name + " 生成 Java 文件");
                            string fileNameWithoutExtension = Path.GetFileName(item.Path);
                            out_dir = Path.GetDirectoryName(item.Path);
                            if (string.IsNullOrWhiteSpace(CreateBase.protobufjavaMessage))
                            {
                                if (!System.IO.Directory.Exists(out_dir + "/out/java/"))
                                {
                                    System.IO.Directory.CreateDirectory(out_dir + "/out/java/");
                                }
                                process.StartInfo.Arguments = "--proto_path=" + out_dir + " --java_out=" + out_dir + "/out/java   " + item.Path;
                            }
                            else
                            {
                                if (!System.IO.Directory.Exists(CreateBase.protobufjavaMessage))
                                {
                                    System.IO.Directory.CreateDirectory(CreateBase.protobufjavaMessage);
                                }
                                process.StartInfo.Arguments = "--proto_path=" + out_dir + " --java_out=" + CreateBase.protobufjavaMessage + "   " + item.Path;
                            }
                            process.Start();
                            string error = process.StandardError.ReadToEnd();
                            if (!string.IsNullOrWhiteSpace(error))
                            {
                                show("java文件:" + fileNameWithoutExtension + "   错误:" + error);
                                return;
                            }
                        }
                    }
                    //process.WaitForExit();
                    process.Close();
                    show("");
                    show("");
                    show("创建 protobuf 2 Java 代码完成 目录：" + out_dir);
                    show("");
                    //System.Diagnostics.Process.Start(CreateBase.protobufjavaMessage);
                }
            }
            catch (Exception e)
            {
                show(e.Message);
            }
        }
    }
}
